﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_AgencyAffirm : System.Web.UI.Page
{
    int projectid;


    protected void Page_Load(object sender, EventArgs e)
    {
        //   projectid = 1;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            //开启全部投标企业投标开关,BiddingSwitching=0为关闭BiddingSwitching=1为开启
            if (Request.Form["Btnopenall"] == "全部开启")
            {
                Db db = new Db();
                string str = "update Affirm set submit='1'";
                db.Command(str);

                db.Close();
                //HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", str, "../Agency/BidSwitch.aspx");

            }

            //关闭全部投标企业投标开关,BiddingSwitching=0为关闭BiddingSwitching=1为开启
            if (Request.Form["Btncloseall"] == "全部关闭")
            {
                Db db = new Db();
                string str = "update Affirm set submit='0' ";
                db.Command(str);
                db.Close();
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", str, "../Agency/BidSwitch.aspx");

            }
            if (Request.Form["B1"] == "开启")
            {
                openbj();
            }
            if (Request.Form["B2"] == "关闭")
            {
                closebj();
            }
            if (Request.Form["butPrint"] == "打印")
            {
                printaffirm();
            }
            
            showdata();
        }


    }
    /// <summary>
    /// datagridview绑定数据
    /// </summary>
    protected void showdata()
    {
        string _strWhere = "";
        if (ComPanyName.Text.Trim() != "")
        {
            _strWhere = "(companyname_tb like '%" + ComPanyName.Text.Trim() + "%' or usercode_tb like '%" + ComPanyName.Text.Trim() + "%')";
        }
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "affirm";                                               //要查询的表明
        dbPage.SelectFieldGroup = " companyname_tb,usercode_tb,submit";
        dbPage.Key = "usercode_tb";                                               //表的主键    

        dbPage.Where = _strWhere;                                           //查询条件    
        dbPage.SelectField = " companyname_tb,usercode_tb,submit";//要显示的字段
            dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
            dbPage.PageSize = 30;                                                    //没有显示记录条数   
            dbPage.OrderBy = "companyname_tb";                  //排序字段
            dbPage.OrderByIsDesc = true;
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //  Response.Write("<p align=center>投标人审核界面</p>");
        sb.Append("<table border=1 cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='98%'>");
        sb.Append("<tr>");
        sb.Append("<td class='table_title' nowrap>选取</td>");
        sb.Append("<td class='table_title' nowrap>投标人</td>");
        sb.Append("<td  class='table_title' nowrap>投标人注册号</td>");
        sb.Append("<td  class='table_title' nowrap>开关状态</td>");
        //     sb.Append("<td nowrap align=center>选择</td>");
        sb.Append("</tr>");

        int intNum = 0, i, j = 0;
        string zhuangtai = "";//zhuangtai 表示审核状态

         while (dr.Read())
        {
            intNum++;
            i = SysFun.ToInt(dr["submit"]);
            if (i == 0)
            {
                zhuangtai = "关";
            }
            if (i == 1)
            {
                zhuangtai = "开";
            }
            if (j == 0)
            {
                sb.Append("<tr class='table_td'>");
                j++;
            }
            else
            {
                sb.Append("<tr class='table_td2'>");
                j--;
            }

            sb.Append("<td><input  type='checkbox' name='id'value='" + SysFun.ToTrim(dr["usercode_tb"]) + "'></td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["companyname_tb"]) + "</td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["usercode_tb"]) + "</td>");
            sb.Append("<td align='center'>" + zhuangtai + "</td>");
            //      sb.Append("<td>" + SysFun.ToTrim(dr["SpellingCode"]) + "</td>");
            sb.Append("</tr>");
        }
        dr.Close();
        dr.Dispose();

        sb.Append("</table>");
        dbPage.Db.Close();
        db.Close();
 
        this.DbPageFlip1.DbPage = dbPage;

        //sb.Append("<p align='right'>" + dbPage.GetPageLink("BidTableProductSelect.aspx?PG={PageNum}" + strGetPageLink) + "</p>");

        //Label lab = new Label();
        Label1.Text = sb.ToString();
        //frmMain.Controls.Add(lab);
       
    }
    /// <summary>
    /// 开启选定企业报价开关
    /// </summary>
    protected void openbj()
    {

        if (Request.Form["id"] != null)
        {
            int intProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["id"]));
            string[] strId = Request.Form["id"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            if (strId.Length > 0)
            {
                string strSql, sql = "";
                Db db = new Db();

                foreach (string Id in strId)
                {
                    try
                    {
                        strSql = "'" + SysFun.ToTrim(Id) + "'";
                        sql = strSql + "," + sql;

                    }
                    catch { }
                }
                sql = sql.Substring(0, sql.Length - 1);
                sql = "update Affirm set submit='1' where  usercode_tb  in (" + sql + ")";
                db.Command(sql);
                db.Close();
                //Response.Redirect("PriceSwitch.aspx?projectid=" + projectid);
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Agency/PriceSwitch.aspx");

            }
            else
            {
                Response.Write("");
            }

        }
    }
    /// <summary>
    /// 关闭选定企业报价开关
    /// </summary>
    protected void closebj()
    {
        if (Request.Form["id"] != null)
        {
            int intProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["id"]));
            string[] strId = Request.Form["id"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            if (strId.Length > 0)
            {
                string strSql, sql = "";
                Db db = new Db();

                foreach (string Id in strId)
                {
                    try
                    {
                        strSql = "'" + SysFun.ToTrim(Id) + "'";
                        sql = strSql + "," + sql;

                    }
                    catch { }
                }
                sql = sql.Substring(0, sql.Length - 1);
                sql = "update Affirm set submit='0' where  usercode_tb  in (" + sql + ")";
                db.Command(sql);
                db.Close();
                //Response.Redirect("PriceSwitch.aspx?projectid=" + projectid);
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Agency/PriceSwitch.aspx");

            }
            else
            {
                Response.Write("");
            }

        }
    }
    public void printaffirm()
    {
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "affirm";
        dbPage.Key = "goodsid";
        dbPage.Where = "";                        //
        dbPage.SelectField = "*";
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 1;//Request.QueryString["PS"]);
        dbPage.OrderBy = " goodsid ";
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        Response.Clear();
        Response.Buffer = true; 
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=affirm.xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行
        Response.End();


    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }
}
